package com.qfhd.soft.cjzcore.TaskCenter.GDT;

import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;

import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Listener.CJAdListener;
import com.qfhd.soft.cjzcore.TaskCenter.CJAdCenter;
import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;

public class CJGDT_RewardVideo implements RewardVideoADListener {

    private static boolean isOpenVoice = true;
    private RewardVideoAD rewardVideoAD;
    private CJAdListener statusListener;

    private static CJGDT_RewardVideo rewardVideo;
    public static CJGDT_RewardVideo getInstance() {
        if (rewardVideo == null) {
            synchronized (CJGDT_RewardVideo.class) {
                rewardVideo = new CJGDT_RewardVideo();
            }
        }
        return rewardVideo;
    }

    public void show(Activity activity, Context context, CJAdListener statusListener){
        this.statusListener = statusListener;
        rewardVideoAD = new RewardVideoAD(context, CJGDTsdk.APPID, CJGDTsdk.RewardVideo, this, isOpenVoice);
        // 2. 加载激励视频广告
        rewardVideoAD.loadAD();
    }

    private void showVideo(){
        // 3. 展示激励视频广告
        if (rewardVideoAD != null)
        {//广告展示检查1：广告成功加载，此处也可以使用videoCached来实现视频预加载完成后再展示激励视频广告的逻辑
            if (!rewardVideoAD.hasShown()) {//广告展示检查2：当前广告数据还没有展示过
                long delta = 1000;//建议给广告过期时间加个buffer，单位ms，这里demo采用1000ms的buffer
                //广告展示检查3：展示广告前判断广告数据未过期
                if (SystemClock.elapsedRealtime() < (rewardVideoAD.getExpireTimestamp() - delta)) {
                    rewardVideoAD.showAD();
                } else {
                    CJLogger.v("GDT 激励视频广告已过期，请再次请求广告后进行广告展示！" );
                }
            } else {
                CJLogger.v("GDT 此条广告已经展示过，请再次请求广告后进行广告展示！" );
            }
        } else {
            CJLogger.v("GDT 成功加载广告后再进行广告展示！" );
        }
    }

    @Override
    public void onADLoad() {
        showVideo();
    }

    @Override
    public void onVideoCached() {

    }

    @Override
    public void onADShow() {

    }

    @Override
    public void onADExpose() {

    }

    @Override
    public void onReward() {
        if(statusListener != null){
            statusListener.onStatus(CJAdCenter.AD_Status_Reward);
        }
    }

    @Override
    public void onADClick() {
        if(statusListener != null){
            statusListener.onStatus(CJAdCenter.AD_Status_Click);
        }
    }

    @Override
    public void onVideoComplete() {

    }

    @Override
    public void onADClose() {
        if(statusListener != null){
            statusListener.onStatus(CJAdCenter.AD_Status_Close);
        }
    }

    @Override
    public void onError(AdError adError) {

    }
}
